import {post, fetch, _platform_id, Multiplication, prefix} from './iFetch'
import {FEE, BASENUMBER,FEE_ADDRESS,MODE} from '@/types/constant'

console.log("api",prefix)
console.log("代收地址",FEE_ADDRESS)
console.log("代码模式",MODE)
//提交申请
export const ApplyConfirm = (param: any) => {
    param.platform_id = _platform_id;
    return post(prefix + "/interface/issue-coin/apply", param)
};

//验证接口
export const getValidate = (param: any) => {
    return post(prefix + "/interface/issue-coin/validate", param)
}

//确认发行
export const confirmApply = (param: any) => {
    return post(prefix + "/interface/issue-coin/verify", param)
}

//取消发行
export const cancelApply = (param: any) => {
    return post(prefix + "/interface/issue-coin/revoke", param)
}

//查询平行链列表
export const getParaList = (param?: any) => {
    return fetch(prefix + "/interface/issue-chain", param)
}

//查询钱包余额
export const getBalance = (address: string, coins: string) => {
    return fetch(prefix + "/interface/wallet/get-balance", {platform_id: _platform_id, address: address, symbol: coins})
}

//查询申请列表
export const getApplyList = (param: any) => {
    return fetch(prefix + "/interface/issue-coin/apply-list", param)
}

//查看申请详情
export const getApplyDetail = (param: any) => {
    return fetch(prefix + "/interface/issue-coin/apply-detail", param)
}

//查询是否需要人工审核
export const getReviewStatus = () => {
    return fetch(prefix + "/interface/issue-chain/manual-review-status")
}

//使用token合约构造未签名交易
export const CreateRawTokenPreCreateTx = (param: any) => {
    const chain33Param = {
        "jsonrpc": "2.0",
        "method": "token.CreateRawTokenPreCreateTx",
        "params": [{
            "name": param.name,
            "symbol": param.symbol,
            "introduction": param.introduction,
            "total": Number(param.total) * BASENUMBER * 1e8,
            "price": FEE,
            "category": param.category,
            "owner": param.owner
        }]
    }
    return post("/parallelApi", chain33Param)
}

//使用token合约生成取消交易
export const setCreateRawTokenRevokeTx = (param: any) => {
    const chain33Param = {
        "jsonrpc": "2.0",
        "method": "token.CreateRawTokenRevokeTx",
        "params": [{
            "symbol": param.symbol,
            "owner": param.owner
        }]
    }
    return post("/parallelApi", chain33Param)
}

//使用token合约增发未签名交易
export const setCreateRawTokenMintTx = (param: any) => {
    const chain33Param = {
        "jsonrpc": "2.0",
        "method": "token.CreateRawTokenMintTx",
        "params": [{
            "symbol": param.symbol,
            "amount": Number(param.total) * BASENUMBER * 1e8,
        }]
    }
    return post("/parallelApi", chain33Param)
}

//使用平行链发送签名后的交易
export const setSendRawTransaction = (data: string) => {
    const chain33Param = {
        "jsonrpc": "2.0",
        "method": "Chain33.SendTransaction",
        "params": [{
            "data": data
        }]
    }
    return post("/parallelApi", chain33Param)
}

//设置扣除手续费
export const setFeeCreateRawTransaction = (coinType: string, amuont: number, exec?: string, isToken?: Boolean) => {
    let paramDetail = null;
    if (isToken) {
        paramDetail = [{
            "to": FEE_ADDRESS,
            "amount": Multiplication(amuont, 1e8),
            "isWithdraw": false,
            "execName": "",
            "fee": 0.001 * 1e8,
            "execer": exec,
        }]
    } else if (!isToken) {
        paramDetail = [{
            "to": FEE_ADDRESS,
            "amount": Multiplication(amuont, 1e8),
            "isWithdraw": false,
            "isToken": true,
            "tokenSymbol": coinType,
            "fee": 0.001 * 1e8,
            "execer": exec,
        }]
    }
    const chain33Param = {
        "jsonrpc": "2.0",
        "method": "Chain33.CreateRawTransaction",
        "params": paramDetail
    }
    console.log(chain33Param)
    return post("/parallelApi", chain33Param)
}

//主链上发送交易
export const setChain33SendRawTransaction = (data: string) => {
    const chain33Param = {
        "jsonrpc": "2.0",
        "method": "Chain33.SendTransaction",
        "params": [{"data": data}]
    }
    return post("/chain33Api", chain33Param)
}


//主链上的查询
export const queryBTYTransaction = (data: string) => {
    const chain33Param = {
        "jsonrpc": "2.0",
        "method": "Chain33.QueryTransaction",
        "params": [{"hash": data}]
    }
    return post("/chain33Api", chain33Param)
}


//构造扣手续费为bty
export const setBTYCreateRawTransaction = (param: any) => {
    let fee = Number(param.issue_charge)
    const chain33Param = {
        "jsonrpc": "2.0",
        "method": "Chain33.CreateRawTransaction",
        "params": [
            {
                "to": FEE_ADDRESS,
                "amount": Multiplication(fee, 1e8),
                "fee": 0.001 * 1e8,
                "isWithdraw": false,
                "execName": ""
            }
        ]
    }
    return post("/chain33Api", chain33Param)
}

